const Actions = require('./actions');
const DB = require('app/db.js');
import { Toast } from 'saltui';
import { monStorage, Storage } from 'clientConfig/util/StoreData';
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        allData: {},
        allSelectData: {},
        top_select: false,
        alltop_select: {}
    },
    onFetch: function (params, cb) {
        let t = this;
        if (params.classId && t.data.allData && t.data.allData.hasOwnProperty(params.classId)) {
            t.data.listData = t.data.allData[params.classId];
            t.data.selectData = t.data.allSelectData[params.classId];
            t.data.allSelectData[params.classId] = t.data.selectData;
            t.data.top_select = t.data.alltop_select[params.classId];
            t.updateComponent()
            return;
        }
        if (params.classId) {
            DB.Notice.showNotice_member_inClass(
                {
                    schoolId: Storage.get('user.schoolId'),
                    classId: params.classId
                }
            ).then((content) => {
                let classId = params.classId;
                if (content.code == 1) {
                    if (content.data && content.data.dataList.length > 0) {
                        for (let [index, item] of content.data.dataList.entries()) {
                            item.checked = false;
                            item.text = item.studentName;
                            item.disable = false;
                            item.value = index;
                        }
                    }
                    t.data.listData = content.data.dataList;
                    t.data.allData[classId] = content.data.dataList;
                    t.data.allSelectData[classId] = [];
                    t.data.alltop_select[classId] = false;
                    t.data.top_select = false;
                    t.updateComponent()
                }
            }).catch((error) => { // 失败 or 有异常被捕获
                console.log(error)
                Toast.hide(() => {
                });
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            })
        }
    },
    onChangeData(selectedData, classId, type = 0) {
        let t = this;
        if (type && selectedData.length == 0) {
            t.data.listData = t.data.allData[classId];
            for (let item of t.data.listData) {
                if (type == 1) {
                    t.data.top_select = true;
                    if (!item.checked) {
                        item.checked = true;
                        continue;
                    }
                } else if (type == 2) {
                    t.data.top_select = false;
                    if (item.checked) {
                        item.checked = false;
                        continue;
                    }
                }
            }
            t.data.alltop_select[classId] = t.data.top_select;
            t.data.allData[classId] = t.data.listData;
            t.data.selectData = type == 1 ? t.data.allData[classId] : [];
            t.data.allSelectData[classId] = t.data.selectData;
            t.updateComponent();
            return;
        }
        if (selectedData && classId) {
            if (selectedData.length == t.data.allData[classId].length || selectedData.length == 0) {
                t.data.top_select = selectedData.length ? true : false;
                t.data.alltop_select[classId] = t.data.top_select;
                return;
            }
            t.data.allSelectData[classId] = selectedData;
            t.data.selectData = selectedData;
            for (let item of t.data.allData[classId]) {
                for (let subitem of selectedData) {
                    if (subitem.studentId == item.studentId) {
                        item.checked = true;
                        continue;
                    }
                }
            }
        }
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
